package kdt

import (
	"bytes"
	"crypto/md5"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strconv"
	"time"
)

type Product struct {
	Title, Name, Intro, ProductId string
	Price                         int
}
type Pack struct {
	PackId       string
	ProductId    string
	Title        string
	Num          int
	DeliveryDots []Dot
	PackState    bool
	Owner, Admin string
}
type Dot struct {
	Title string
	Time  time.Time
}
type User struct {
	Uid, Sid string
	Phone    string
	Password string
}
type Admin struct {
	Uid, Sid, Phone, Password string
}
type Msg struct {
	State, Info, Other string
}

var (
	MongoDBServer = "127.0.0.1"
)

func ReturnInfo(w http.ResponseWriter, state, info string) {
	b, _ := json.Marshal(Msg{State: state, Info: info})
	w.Write(b)
}
func ReturnData(w http.ResponseWriter, data interface{}) {
	b, _ := json.Marshal(data)
	w.Write(b)
}
func DoRequest(url string, i interface{}) ([]byte, error) {
	b, _ := json.Marshal(i)
	rp, e := http.Post(url, "text/plain", bytes.NewReader(b))
	if e != nil {
		return nil, e
	}
	defer rp.Body.Close()
	data, _ := ioutil.ReadAll(rp.Body)
	return data, nil
}
func NewToken() string {
	c := time.Now().Unix()
	h := md5.New()
	io.WriteString(h, strconv.FormatInt(c, 10))
	return fmt.Sprintf("%x", h.Sum(nil))
}
func crossDomain(w http.ResponseWriter) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
}
